<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <script src="js/mui.js"></script>
    <link href="css/mui.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="css/header.css"/>

    <style>

        #scanComponent {
            width: 100%;
            top: 44px;
            bottom: 0px;
            position: absolute;
            text-align: center;
            background: #000000;
        }

    </style>
</head>

<body>

<header class="mui-bar mui-bar-nav title" style="position: fixed;">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left title-color"></a>
    <h1 class="mui-title title-color"><b id="chatting-nickname">扫一扫</b></h1>
</header>

<div id="scanComponent"></div>

<script type="application/javascript" src="js/app.js"></script>
<script type="text/javascript">
    mui.init();

    mui.plusReady(function () {

        setTimeout("startScan()", "500");

    });

    var scan;

    function startScan() {

        var styles = {
            frameColor: "#128E12",
            scanbarColor: "#0062CC",
            background: ""
        };

        scan = new plus.barcode.Barcode('scanComponent', null, styles);
        scan.onmarked = onmarked;
        scan.start();
    }

    function onmarked(type, result) {
        if (type === 0) {
            if (app.isNotNull(result)) {

                plus.nativeUI.showWaiting("请稍后");
                var user = app.getUserGlobalInfo();
                mui.ajax(app.serverUrl + "/user/scanQrCodeSearchUserById", {
                    data: {

                        myUserId: user.id,
                        friendId: result
                    },
                    dataType: 'json',//服务器返回json格式数据
                    type: 'post',//HTTP请求类型
                    timeout: 10000,//超时时间设置为10秒；
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    success: function (data) {
                        //服务器返回响应，根据响应结果，分析是否登录成功；
                        console.log(JSON.stringify(data));
                        // 关闭等待框
                        plus.nativeUI.closeWaiting();
                        //判断状态
                        if (data.status == 200) {

                            //跳转页面
                            mui.openWindow({
                                url: "searchFriendsResult.html",
                                id: "searchFriendsResult.html",
                                styles: {},
                                extras: {
                                    friend: null,
                                    result: data.data
                                }
                            });
                        } else {
                            app.showToast(data.msg, "error");
                            return false;
                        }
                    },
                    error: function (data) {
                        // 关闭等待框
                        plus.nativeUI.closeWaiting();
                        //提示信息
                        app.showToast("服务器异常请稍后重试", "error");
                    }
                });
            } else {
                app.showToast("二维码错误", "error");
            }
        } else {
            alert(result);
        }
        scan.close();
        startScan();
    }
</script>
</body>

</html>